/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.biviz.jgooglegears.jdbc;

import com.biviz.jgooglegears.JGGStubApplet;
import java.applet.Applet;
import java.sql.*;
import java.util.Properties;
import netscape.javascript.JSObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import textbender.o.rhinohide.RhiWindow;
import textbender.o.rhinohide.Rhinohide;

/**
 *
 * @author zhengyu.jin
 */
public class JGoogleGearsDriver implements Driver {

    private static final String PREFIX = "jdbc:jgooglegears";


    static {
        try {
            DriverManager.registerDriver(new JGoogleGearsDriver());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private void makeSureGears_init_js(Document document) {
        NodeList headList = document.getElementsByTagName("head");

        if (headList.getLength() == 1) {
            Element head = (Element) headList.item(0);
            NodeList scriptList = head.getElementsByTagName("script");

            Boolean bHas_gears_init_js = false;
            for (int i = 0; i < scriptList.getLength(); i++) {
                Element eachScript = (Element) scriptList.item(i);
                String src = eachScript.getAttribute("src");
                if (src.length() > 0) {
                    if (src.contains("gears_init.js")) {
                        bHas_gears_init_js = true;
                    }
                }
            }
            if (!bHas_gears_init_js) {
                System.out.println("No script element includes gears_init.js, it's been appended in header within Applet");
                Element script_gears_init = document.createElement("script");
                script_gears_init.setAttribute("src", "gears_init.js");
                head.appendChild(script_gears_init);
            }
        }
    }

    public Connection connect(String url, Properties info) throws SQLException {
        if (!acceptsURL(url)) {
            new SQLException("Unsupported URL");
        }

        RhiWindow window = JGGStubApplet.getWindowByUUID(info.getProperty(JGGStubApplet.APPLET_INST_UUID_KEY));
        Document document = window.getDocument();
        
        String dbName = url.substring(PREFIX.length());
        dbName = dbName.substring(dbName.indexOf(":") + 1);

        assert document.getImplementation().hasFeature("Core", "2.0");

        Rhinohide db = new Rhinohide(window, (JSObject) window.eval("google.gears.factory.create('" + dbName + "')"));
        db.call("open", "database-test");

        return new JGGConnection();
    }

    public boolean acceptsURL(String url) throws SQLException {
        return url != null && url.toLowerCase().startsWith(PREFIX);
    }

    public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getMajorVersion() {
        return 1;
    }

    public int getMinorVersion() {
        return 0;
    }

    public boolean jdbcCompliant() {
        return false;
    }
}
